home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13702 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: mail2news.demon.co.uk!vitesse.demon.co.uk
  2. From: Rich Shepard <Rich@vitesse.demon.co.uk>
  3. Newsgroups: comp.lang.c++
  4. Subject: Can't Get A HANDLE (VC++)
  5. Date: Tue, 26 Mar 96 23:22:41 GMT
  6. Organization: Myorganisation
  7. Message-ID: <827882561snz@vitesse.demon.co.uk>
  8. Reply-To: Rich@vitesse.demon.co.uk
  9. X-NNTP-Posting-Host: vitesse.demon.co.uk
  10. X-Newsreader: Demon Internet Simple News v1.30
  11. X-Mail2News-Path: relay-1.mail.demon.net!punt.demon.co.uk!vitesse.demon.co.uk
  12.  
  13. Hi There,
  14.  
  15. Sorry about the pun, but I've been trying to redirect the Standard output of a 
  16. child process so that a parent process can read it. It all looks so simple, 
  17. yeah I've done this before (a while ago but.... I understand the basics... 
  18. don't I).
  19.  
  20. It just doesn't seem to work. The Duplicate handle line always fails. If I miss 
  21. this out the process is spawned but the output is not redirected and the Read 
  22. from pipe at the end just hangs the process!!!! ARGH it looks so simple.
  23.  
  24. Seems to me that all the examples work fine, it's just when I try to 
  25. incorporate them into my code they don't work. Maybe it's time to quit 
  26. computing and become a bus driver???:)
  27.  
  28. Thanks In Advance. I would be so grateful if someone would help. You'd 
  29. definately be on my Christmas card list:)
  30.  
  31. Rich.
  32.  
  33. //THE NIGHTMARE CODE FOLLOWS!!!!!!!!!!!!!!!!!!!!!!
  34.  
  35. #define BUFSIZE 2048
  36.  
  37. void CThreadDlg::OnButton1() 
  38. {
  39.     // TODO: Add your control notification handler code here
  40.     BOOL bSuccess;
  41.     BOOL Flag;
  42.     HANDLE pin;
  43.     HANDLE pout;
  44.     HANDLE DefIn;
  45.     HANDLE DefErr;
  46.     HANDLE DefStd;
  47.     PROCESS_INFORMATION piProcInfo;
  48.     STARTUPINFO siStartInfo;
  49.     SECURITY_ATTRIBUTES saAttr;
  50.     DWORD dwRead;
  51.     CHAR chBuf[BUFSIZE];
  52.  
  53.  
  54.     saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  55.     saAttr.bInheritHandle = TRUE;
  56.     saAttr.lpSecurityDescriptor = NULL;
  57.  
  58.  
  59.     //Set up members of STARTUPINFO structure. 
  60.  
  61.     siStartInfo.cb = sizeof(STARTUPINFO);
  62.     siStartInfo.lpReserved = NULL;
  63.     siStartInfo.lpReserved2 = NULL;
  64.     siStartInfo.cbReserved2 = 0;
  65.     siStartInfo.lpDesktop = NULL;
  66.     siStartInfo.dwFlags = 0;
  67. //    siStartInfo.dwFlags = STARTF_USESTDHANDLES;
  68.  
  69.     // Create the child process. 
  70.  
  71.     bSuccess = DuplicateHandle(GetCurrentProcess(),
  72.     GetStdHandle(STD_INPUT_HANDLE),
  73.     GetCurrentProcess(),
  74.     NULL,
  75.     0,
  76.     FALSE,DUPLICATE_SAME_ACCESS);
  77.     
  78.     if (!CreatePipe(&pin,&pout,&saAttr,1024))
  79.     {
  80.         MessageBox("Pipe Creation Failed");     
  81.     }
  82.  
  83.     DefStd = GetStdHandle(STD_OUTPUT_HANDLE);
  84.     DefIn=GetStdHandle(STD_INPUT_HANDLE);
  85.     DefErr=GetStdHandle(STD_ERROR_HANDLE);
  86.     if (!CreatePipe(&pin, &pout, &saAttr, 0))
  87.     {
  88.         MessageBox("Create Pipe Failed");
  89.     }
  90.     if (!SetStdHandle(STD_OUTPUT_HANDLE, pout))
  91.     {
  92.         MessageBox("Redirect Failed");
  93.     }
  94.     if (CreateProcess(NULL,
  95.     "\\MYAPPLICATION",       //command line                       
  96.  
  97.     NULL,          // process security attributes        
  98.     NULL,          // primary thread security attributes 
  99.     TRUE,          // handles are inherited              
  100.     0,             // creation flags                     
  101.     NULL,          // use parent's environment           
  102.     NULL,          // use parent's current directory     
  103.     &siStartInfo,  // STARTUPINFO pointer                
  104.     &piProcInfo)==FALSE)  // receives PROCESS_INFORMATION
  105.     {
  106.         MessageBox("Process Failed");
  107.     }
  108.     
  109.     if (!SetStdHandle(STD_OUTPUT_HANDLE, DefStd))
  110.     {
  111.         MessageBox("StdOut Restore Failed");
  112.     }
  113.  
  114.     Flag=TRUE;
  115.     while (Flag==TRUE)
  116.     {
  117.         
  118.  
  119.         Flag=ReadFile(pin, chBuf, BUFSIZE, &dwRead, NULL);
  120.         if (dwRead == 0)
  121.         {
  122.             Flag=FALSE;
  123.         }
  124.         if (Flag==TRUE)
  125.         {
  126.             MessageBox(chBuf);
  127.         }
  128.     }  
  129.  
  130. }
  131.  
  132.  
  133. -- 
  134. Rich Shepard
  135.